package com.hnucm.cooklife.model;

import android.os.Environment;

import com.hnucm.cooklife.base.BaseModel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;

public class TestModel extends BaseModel {
    private void postGoodsPicToServer(){
        Map<String, RequestBody> params = new HashMap<>();
        //以下参数是伪代码，参数需要换成自己服务器支持的
        params.put("type", convertToRequestBody("type"));
        params.put("title",convertToRequestBody("title"));
        params.put("info",convertToRequestBody("info"));
        params.put("count",convertToRequestBody("count"));

        //storage/emulated/0
        //为了构建数据，同样是伪代码/storage/emulated/0/Pictures
        //File.separator分割符
        String path1 = Environment.getExternalStorageDirectory() + File.separator + "Pictures" + File.separator + "test.png";
//        String path2 = Environment.getExternalStorageDirectory() + File.separator + "test1.jpg";

        List<File> fileList = new ArrayList<>();

        fileList.add(new File(path1));
//        fileList.add(new File(path2));

        List<MultipartBody.Part> partList = filesToMultipartBodyParts(fileList);

        mApiServer.postGoodsReturnPostEntitys(params,partList);
    }

    private RequestBody convertToRequestBody(String param){
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), param);
        return requestBody;
    }

    private List<MultipartBody.Part> filesToMultipartBodyParts(List<File> files) {
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (File file : files) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("multipartFiles", file.getName(), requestBody);
            parts.add(part);
        }
        return parts;
    }
}
